// 库安全详情未开启页面
import React, { Component } from 'react';
import { Tag } from '@teamix/ui';
import TextAvatar from '@txdfe/at-text-avatar';
import track from 'utils/track';
import SettingModal from 'components/projectSecuritySettingModal';
import './index.scss';
import intl from '../../locale';

const prefix = 'disabled-project-security';
export default class DisabledProjectSecurityDetail extends Component {
  state = {
    isModalOpen: false,
  };

  closeModal = () => {
    this.setState({
      isModalOpen: false,
    });
  };

  goSetting = () => {
    const { isAdmin, plugin } = this.props;
    if (!isAdmin) {
      return;
    }

    this.setState({
      isModalOpen: true,
    });

    // navigate(this.props.plugin.setting_url, { state: { forceUpdate: true } });

    track({
      action:
        plugin.key === 'dependency_analysis'
          ? 'turn_on_dependency_check'
          : 'turn_on_sensitive_info_check',
      page: 'Repo-Security',
      type: '',
      control: '',
    });
  };

  lookMore = () => {
    track({
      action:
        this.props.plugin.key === 'dependency_analysis'
          ? 'know_more_dependency_check'
          : 'know_more_sensitive_info_check',
      page: 'Repo-Security',
      type: '',
      control: '',
    });
  };

  render() {
    const { isAdmin, plugin, serviceData } = this.props;
    const { isModalOpen } = this.state;
    const { avatar_url, name, description, desc_url, key, disablePageImg } = plugin;
    return (
      <div className={`${prefix}`}>
        {key === 'pinpoint_detection' ? (
          <div className="pippoint-disable-img">
            <img src={disablePageImg} />
          </div>
        ) : (
          <div className={`${prefix}-img`}>
            <TextAvatar text={name} image={avatar_url} radius="4px" />
          </div>
        )}

        <div className={`${prefix}-name`}>
          {name}
          {plugin.betaText && (
            <Tag type="primary" style={{ margin: '0 0 0 4px' }}>
              {plugin.betaText}
            </Tag>
          )}
        </div>
        <div className={`${prefix}-desc`}>{description}</div>
        <div className={`${prefix}-btn`}>
          <span
            onClick={this.goSetting}
            className={`${prefix}-btn-${isAdmin ? 'primary' : 'disabled'}`}
          >
            {isAdmin
              ? intl.get({
                  id: 'code-assets.components.projectSecurityDisabledDetail.EnableNow',
                  defaultMessage: '立即启用',
                })
              : intl.get({
                  id: 'code-assets.components.projectSecurityDisabledDetail.ContactTheAdministratorToEnable',
                  defaultMessage: '联系管理员开启',
                })}
          </span>
        </div>
        <div className={`${prefix}-more`}>
          {desc_url ? (
            <a href={plugin.desc_url} target="_blank" onClick={this.lookMore} rel="noreferrer">
              {intl.get({
                id: 'code-assets.components.projectSecurityDisabledDetail.LearnMore',
                defaultMessage: '了解更多',
              })}
            </a>
          ) : null}
        </div>
        <div className={`${prefix}-bg`} />
        {isModalOpen && (
          <SettingModal
            closeModal={this.closeModal}
            isModalOpen={isModalOpen}
            modalPlugin={plugin}
            data={serviceData}
          />
        )}
      </div>
    );
  }
}
